﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.ServiceModel.Syndication;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Cofe.Core;
using Cofe.Core.Implements;
using Cofe.Data;
using Cofe.Web.Utils;
using Newtonsoft.Json;

namespace Cofe.Web
{
    [System.Obsolete]
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFEWS)]
    public class SyndicationItemBuilder : ISyndicationItemBuilder
    {
        public SyndicationItem BuildSyndicationItem(CofeSystemEventLog log)
        {
            var type = log.ChangeTypeTyped.ToString().ToLower();
            var entryType = log.IsFolder ? "folder" : "file";
            var title = string.Format("{0} {1} is {2} ", entryType, log.EntryParsePath, type);
            //var selfUrl = "/
            //    Url.Route("DefaultApi",
            //    new { httproute = "", controller = "events", resource = log.Id.Value });

            CofeSystemInfo entry = EntryFE.TryParsePathAsync(log.EntryParsePath, ParsableType.ParsePath).Result as CofeSystemInfo;
            var entryId = log.EntryId.HasValue ? log.EntryId.Value : entry != null ? entry.Id : Guid.Empty;
            var entryUrl = entryId != Guid.Empty ?
                String.Format("/entry/{0}", entryId) :
                //Url.Route("DefaultApi", new { httproute = "", controller = "entry", id = entryId }) :
                "";

            var retVal = new SyndicationItem()
            {
                Id = log.Id.ToString(),
                Title = new TextSyndicationContent(title),
                LastUpdatedTime = log.Time.ToLocalTime()
            };

            retVal.Authors.Add(new SyndicationPerson(log.UserId));
            retVal.Categories.Add(new SyndicationCategory("type/" + type));
            retVal.Categories.Add(new SyndicationCategory("entry/" + entryType));

            if (entry != null)
            {
                var jObject = CofeServices.EntrySerializer.WriteEntry(entry, false, false);
                var xmlDocument = JsonConvert.DeserializeXmlNode(jObject.ToString(), "Entry");
                using (MemoryStream ms = new MemoryStream())
                {
                    xmlDocument.Save(ms); ms.Seek(0, SeekOrigin.Begin);
                    var r = XmlReader.Create(ms);
                    retVal.Content = new XmlSyndicationContent(r);
                }
                //new UrlSyndicationContent(UrlUtils.FormatUri(entryUrl), "application/xml");
            }

            if (!String.IsNullOrEmpty(entryUrl))
                retVal.Links.Add(new SyndicationLink(UrlUtils.FormatUri(entryUrl), "related", null, "application/xml", 0));

            return retVal;
        }

        public IEnumerable<SyndicationItem> BuildSyndicationItem(IEnumerable<CofeSystemEventLog> logs)
        {
            return from log in logs select BuildSyndicationItem(log);
        }
    }
}
